package com.sanjiahantang.youjiaclient.module.evaluate.views.impl;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sanjiahantang.uilibrary.adapter.recyclerview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.recyclerview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.MyGridView;
import com.sanjiahantang.uilibrary.views.TagGroup;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.module.evaluate.adapters.MeEvaluateAdapter;
import com.sanjiahantang.youjiaclient.module.evaluate.beans.EvaGoodsBean;
import com.sanjiahantang.youjiaclient.module.evaluate.beans.Image;
import com.sanjiahantang.youjiaclient.module.evaluate.presenters.MeEvaluatePresenter;
import com.sanjiahantang.youjiaclient.module.evaluate.views.MeEvaluateView;
import com.sanjiahantang.youjiaclient.module.goods.beans.GoodsBean;
import com.sanjiahantang.youjiaclient.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PARAM;

/**
 * Created by zy on 2017/10/30.
 */

public class EVAGoodsListActivity extends BaseMvpActivity<MeEvaluateView,MeEvaluatePresenter> implements MeEvaluateView, View.OnClickListener {


    private RecyclerView recyclerView;
    @Override
    public void loadData(RecyclerView.Adapter adapter) {

    }

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public MeEvaluatePresenter initPresenter() {
        return new MeEvaluatePresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_match_list);
        TextView title = (TextView) findViewById(R.id.title);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        final SwipeRefreshLayout refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshLayout.setRefreshing(false);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        title.setText("商品评价");
        findViewById(R.id.fl_Left).setOnClickListener(this);

        Bundle b = getIntent().getBundleExtra(KEY_PARAM);
        ArrayList<EvaGoodsBean.Item> list = (ArrayList<EvaGoodsBean.Item>) b.getSerializable("list");
        EvaGoodsBean bean = new EvaGoodsBean();
        EvaGoodsBean.Data data = new EvaGoodsBean.Data();
        data.setList(list);
        bean.setData(new Gson().toJson(data));
        QuickAdapter evaluateAdapter = new QuickAdapter<EvaGoodsBean.Item>($this, R.layout.item_eva_goods, list) {
            @Override
            protected void convert(BaseAdapterHelper helper, EvaGoodsBean.Item item) {
                if (item.getReplyPlace() != null){
                    helper.setText(R.id.txtFeedBack,"回复：" + item.getReplyPlace());
                    helper.setVisible(R.id.replyLayout,true);
                }else{
                    helper.setVisible(R.id.replyLayout,false);
                }

                if (item.getCreateTime() != null){
                    helper.setText(R.id.txtCreateTime,item.getCreateTime().substring(0,"yyyy-MM-hh".length()));
                }
                helper.setVisible(R.id.txtPhone,true);
                helper.setText(R.id.txtContent,item.getPlace());
                helper.setText(R.id.txtPhone,item.getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
                if (item.getNumber() != null){
                    RatingBar ratingBar = helper.getView(R.id.ratingBar);
                    try {
                        ratingBar.setRating(Float.parseFloat(item.getNumber()));
                        ratingBar.setIsIndicator(true);
                    }catch (NumberFormatException e){
                        Log.e("rating",e.getMessage());
                    }

                }


                MyGridView gvImageList = helper.getView(R.id.gvImageList);
                if (item.getImgUrlList() != null && item.getImgUrlList().size() > 0){
                    gvImageList.setVisibility(View.VISIBLE);
                    gvImageList.setAdapter(new com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter<GoodsBean.Image>(context,R.layout.item_image,item.getImgUrlList()) {
                        @Override
                        protected void convert(com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper helper, final GoodsBean.Image item) {
                            ImageView iv = helper.getView(R.id.image);
                            Picasso.with(context).load(FileUtils.rePath(item.getUrl())).placeholder(R.drawable.i_rect_bg).into(iv);
                            helper.getView().setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Utils.showPicDialog((Activity) context,FileUtils.rePath(item.getUrl()),R.drawable.i_rect_bg);
                                }
                            });
                        }
                    });
                }else{
                    gvImageList.setVisibility(View.GONE);
                }

                helper.setVisible(R.id.panelGoods,false);
            }
        };
        recyclerView.setAdapter(evaluateAdapter);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fl_Left:
                finish();
                break;
        }
    }
}
